#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>

using namespace std;

void Test_string1()
{
	string s1;					//构造空字符串
	string s2("hello world");	//C格式字符串构造s2
	string s3(s2);				//拷贝构造
}

void Test_string2()
{
	string s("hello world");
	cout << s.size() << ' ' << s.length() << ' ' << s.capacity() << endl;
}


void Test_string3()
{
	string s1;
	string s2("hello world");
	cout << empty(s1) << endl << empty(s2) << endl;
	cout << s2.capacity() <<' '<<s2.size() << endl;
	s2.clear();
	cout << "clear操作:" << endl;
	cout << s2.capacity()<<' '<<s2.size() << endl;
	cout << empty(s2) << endl;
}

void Test_string4()
{
	string s1("hello world");
	cout << "reserve操作:" << endl;
	cout << s1.capacity() << endl;
	s1.reserve(20);
	cout << s1.capacity() << endl;
	s1.reserve(5);
	cout << s1.capacity() << endl;
	cout << "resize操作:" << endl;
	cout <<s1<<' ' << s1.size() << endl;
	s1.resize(15);
	cout << s1 << ' ' << s1.size() <<' '<< s1.capacity() << endl;
	s1.resize(5);
	cout << s1 << ' ' << s1.size() <<' '<< s1.capacity() << endl;
	s1.resize(30, 'a');
	cout << s1 << ' ' << s1.size() << ' ' << s1.capacity() << endl;

}

void Test_string5()
{
	string s("hello world");
	// 3种遍历方式：
	// 需要注意的以下三种方式除了遍历string对象，还可以遍历是修改string中的字符，
	// 另外以下三种方式对于string而言，第一种使用最多
	// 1. for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
	{
		cout << s[i];
	}
	cout << endl;

	// 2.迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;

	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit ;
		++rit;
	}
	cout << endl;
	// 3.范围for
	for (auto ch : s)
	{
		cout << ch;
	}
	cout << endl;

}

void Test_string6()
{
	string s;
	s.push_back(' ');	//插入空格
	s.append("hello");	//追加c-string字符串
	s += 'w';		//追加字符
	s += "orld";	//追加字符串
	cout << s << endl;
	cout << s.c_str() << endl;	//以c语言方式打印字符串
}


void Test_string7()
{
	string filename("test.cpp.tar.zip");
	// 后缀
	size_t pos = filename.rfind('.');
	if (pos != string::npos)
	{
		//string suff = filename.substr(pos, filename.size() - pos);
		string suff = filename.substr(pos);
		cout << suff << endl;
	}
	pos = filename.find('.');
	if (pos != string::npos)
	{
		string suff = filename.substr(pos, filename.size() - pos);
		cout << suff << endl;
	}
}


void Test_string8()
{
	//string s1;
	//cin >> s1;
	//cout << s1 << endl;
	string s2;
	getline(cin, s2);
	cout << s2 << endl;
}

int main()
{
	Test_string7();
	return 0;
}